from flask_restful import fields, reqparse

regist_parser = reqparse.RequestParser()
regist_parser.add_argument('order_id')
regist_parser.add_argument('source_ids', type=list)

sku_fields = {
    'sku_id': fields.String,
    'num': fields.Integer,
    'source_order': fields.String
}

regist_fields = {
    'order_id': fields.String,
    'source_ids': fields.List,
    'business_type': fields.Integer,
    'data': fields.Nested(sku_fields),
    'user_id': fields.String,
    'sys_id': fields.String
}


regist_json_schema = {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "business_type": {
                "type": "integer"
            },
            "user_id": {
                "type": "string"
            },
            "sys_id": {
                "type": "string"
            },
            "order_id": {
                "type": "string"
            },
            "source_ids": {
                "type": "array",
                "items": {
                    "type": "string"
                }
            },
            "data": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "sku_id": {
                            "type": "string"
                        },
                        "num": {
                            "type": "integer"
                        },
                        "source_order": {
                            "array": {
                                "items": {
                                    "type": "string"
                                }
                            }
                        }
                    },
                    "required": ["sku_id", "num"]
                }
            },
        },
        "required": ["business_type",
                     "user_id",
                     "sys_id",
                     "source_ids",
                     "order_id",
                     "data"]
    }
}

if __name__ == "__main__":
    from jsonschema import validate
    data = [
        {
            "order_id": "DDX13025423525",
            "business_type": 26,
            "user_id": "管易",
            "sys_id": "GUANYI",
            "source_ids": ["DDX13025423525"],
            "data": [
                {
                    "sku_id": "504031851",
                    "num": 1
                },
                {
                    "sku_id": "515010040",
                    "num": 1
                }
            ]
        }
    ]
    validate(data, regist_json_schema)
